package com.alibaba.springboot.health.service.impl;

import com.alibaba.springboot.health.dao.UserDao;
import com.alibaba.springboot.health.pojo.User;
import com.alibaba.springboot.health.service.UserService;
import com.alibaba.springboot.health.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    private static final Logger LOGGER= LogManager.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;


    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return userDao.queryById(userId);
    }
    /**
     * 通过用户名查询单条数据
     *
     * @param userName 用户名
     * @return 实例对象
     */
    @Override
    public User queryByName(String userName) {
        LOGGER.info(userDao.queryByName(userName)+"======================");
        return userDao.queryByName(userName);
    }

    /**
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param user 实体
     * @return PageInfo<User> 分页数据集合
     */
    @Override
    public PageInfo<User> queryAll(int page, int pageSize, User user) {
        /*拦截分页信息*/
        PageHelper.startPage(page, pageSize);

        List<User> userList = userDao.queryAll(user);

        return new PageInfo<>(userList);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public UserVo insert(UserVo user) {
        userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public User update(User user) {
        userDao.update(user);
        return queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Integer userId) {
        return userDao.deleteById(userId) > 0;
    }
}